package com.heer.gymnasium.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.constant.Type;
import com.qict.xinba.Util;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * Created by yan
 * on 2018/3/26
 */

public class WxShareUtils {

    private Context context;

    final IWXAPI api;

    public WxShareUtils(Context context) {
        this.context = context;
        api = WXAPIFactory.createWXAPI(context, IConstants.APPID_WX, false);
        api.registerApp(IConstants.APPID_WX);
    }

    //    微信拼单与约战分享到微信的链接：  约战:域名/dist/shareTeam?id=xx  拼单：域名/dist/shareBill?id=xx
    public void wxShare(String id, int type) {

        WXWebpageObject webpageObject = new WXWebpageObject();

        WXMediaMessage wxMediaMessage = new WXMediaMessage(webpageObject);
        if (type == Type.SHARE_MATCH) {
            webpageObject.webpageUrl = IConstants.BASRURL + "/dist/shareTeam?id=" + id;
            wxMediaMessage.title = "战队约战";
            wxMediaMessage.description = "欢迎应战！";
        } else if (type == Type.SHARE_TEAM) {
            webpageObject.webpageUrl = IConstants.BASRURL + "/dist/shareWarTeam?id=" + id;
            wxMediaMessage.title = "战队分享";
            wxMediaMessage.description = "欢迎加入战队！";
        } else if (type == Type.SHARE_FELLOW) {
            webpageObject.webpageUrl = IConstants.BASRURL + "/dist/shareBill?id=" + id;
            wxMediaMessage.title = "分享拼单";
            wxMediaMessage.description = "欢迎加入拼单！";
        } else {
            webpageObject.webpageUrl = IConstants.BASRURL + "/dist/shareEnterStock?id=" + id;
            wxMediaMessage.title = "入场券";
            wxMediaMessage.description = "分享入场券！";
        }

        wxMediaMessage.thumbData = Util.bmpToByteArray(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher), true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "webpage";
        req.message = wxMediaMessage;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }
}
